<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function timer(t) {
            return new Promise(resolve => {
                setTimeout(() => {
                    resolve("data-" + t)
                }, t)
            })
        }
        //实际开发中，可以把timer替换成真实的异步任务，实现js的任务调度功能
        async function* gen() {
            yield timer(1000)
            yield timer(2000)
            yield timer(3000)
        }
        async function test() {
            let g = gen()
            let arr = [g.next(), g.next(), g.next()]
            //for await里面循环的是异步遍历器生成的对象，将异步任务按照串联的形式执行
            for await (let item of arr) {
                console.log("start-", Date.now())
                console.log(item)
                console.log("end-", Date.now())
            }
        }
        test()

    </script>
</body>

</html>